package indi.zhifa.recipe.bailan.busy.auth.controller.api;

import indi.zhifa.recipe.bailan.framework.auth.impl.entity.po.AuthNodeEntity;
import indi.zhifa.recipe.bailan.busy.auth.entity.dto.department.DepartmentCreateDto;
import indi.zhifa.recipe.bailan.busy.auth.entity.dto.department.DepartmentEditDto;
import indi.zhifa.recipe.bailan.busy.auth.service.IAuthNodeService;
import indi.zhifa.recipe.bailan.framework.auth.entity.annotations.auth.RequireRole;
import indi.zhifa.recipe.bailan.framework.auth.entity.annotations.auth.DataPermission;
import indi.zhifa.recipe.bailan.framework.auth.entity.vo.BaseDepartmentNodeVo;
import indi.zhifa.recipe.bailan.framework.auth.entity.vo.BaseDepartmentWithChildrenVo;
import indi.zhifa.recipe.bailan.framework.common.entity.annotations.ZfRestController;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Api(tags = "DeviceDepartmentApi-设备部门接口")
@RequestMapping("/api/department/device")
@Slf4j
@ZfRestController
@RequiredArgsConstructor
public class DeviceDepartmentApi {

    private final IAuthNodeService mDepartmentService;

    @DataPermission(domain = "zf-mgr")
    @RequireRole(roles = {"depMgr"})
    @Operation(summary = "创建部门")
    @PostMapping("/{id}/createChild")
    public AuthNodeEntity create(
            @Parameter(description = "父级Id") @PathVariable(name = "id") Long pParentId,
            @Parameter(description = "创建配置") @RequestBody DepartmentCreateDto pDepartmentCreateDto){
        return mDepartmentService.create(pParentId,pDepartmentCreateDto);
    }

    @DataPermission(domain = "zf-mgr")
    @RequireRole(roles = {"depMgr"})
    @Operation(summary = "修改部门")
    @PutMapping("/{id}")
    public AuthNodeEntity edit(
            @Parameter(description = "id") @PathVariable(name = "id") Long pId,
            @Parameter(description = "修改配置") @RequestBody DepartmentEditDto pDepartmentEditDto){
        return mDepartmentService.edit(pId,pDepartmentEditDto);
    }

    @RequireRole(roles = {"staff"})
    @DataPermission(domain = "zf-mgr")
    @Operation(summary = "查看部门")
    @GetMapping("/{id}")
    public BaseDepartmentWithChildrenVo info(
            @Parameter(description = "id") @PathVariable(name = "id") Long pId){
        return mDepartmentService.infoWithChildren(pId);
    }

    @RequireRole(roles = {"staff"})
    @DataPermission(domain = "zf-account")
    @Operation(summary = "查看部门-树")
    @GetMapping("/{id}/tree")
    public BaseDepartmentNodeVo infoTree(
            @Parameter(description = "id") @PathVariable(name = "id") Long pId){
        return mDepartmentService.infoTree(pId);
    }

    @DataPermission(domain = "zf-mgr")
    @RequireRole(roles = {"accountMgr"})
    @Operation(summary = "删除部门")
    @DeleteMapping("/{id}")
    public String delete(
            @Parameter(description = "id") @PathVariable(name = "id") Long pId){
        mDepartmentService.delete(pId);
        return "删除成功";
    }

    @DataPermission(domain = "zf-mgr")
    @Operation(summary = "选定父节点")
    @PostMapping("/{id}/reparent")
    public BaseDepartmentNodeVo reparent(
            @Parameter(description = "id") @PathVariable(name = "id") Long pId,
            @Parameter(description = "父节点")  @RequestParam(name = "parentId") Long pParentId){
        BaseDepartmentNodeVo departmentVo = mDepartmentService.reRoot(pParentId,pId);
        return departmentVo;
    }

}
